// 引入路由

import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Search from '@/pages/Search'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import MyOrder from '@/pages/Center/MyOrder'
import GroupOrder from '@/pages/Center/GroupOrder'

export default [
    // 重定向
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        // 路由懒加载，当路由被访问的时候才会加载组件，这样就会更加高效
        component: ()=>import('@/pages/Home'),
        meta: {
            isShow: true
        }
    },
    {
        path: '/login',
        component: Login,
        meta: {
            isShow: false
        }
    },
    {
        path: '/register',
        component: Register,
        meta: {
            isShow: false
        }
    },
    {
        path: '/search/:keyword?',
        component: Search,
        meta: {
            isShow: true
        },
        name: 'search',
        // 路由组件能不能传递props数据？
        // 布尔值写法：只能传递params参数
        // props:true
        // 对象写法：额外的给路由组件传递一些props
        // props:{a:1,b:2}
        // 函数写法：
        props: ($route) => ({
            keyword: $route.params.keyword
        })


    },
    {
        path: '/detail/:skuId',
        component: Detail,
        name: 'detail',
        meta: {
            isShow: true
        }
    },
    {
        path: '/addCartSuccess',
        component: AddCartSuccess,
        name: 'addCartSuccess',
        meta: {
            isShow: true
        }
    },
    {
        path: '/shopCart',
        component: ShopCart,
        name: 'shopCart',
        meta: {
            isShow: true
        }
    },
    {
        path: '/trade',
        component: Trade,
        name: 'trade',
        meta: {
            isShow: true
        },
        // 路由独享守卫
        beforeEnter: (to, from, next) => {
            // 如果想去交易页面，必须从购物车过来，否则就重定向到当前路由
            if (from.path == '/shopCart') {
                next()
            } else {
                next(false)
            }
        }
    },
    {
        path: '/pay',
        component: Pay,
        name: 'pay',
        meta: {
            isShow: true
        },
        beforeEnter: (to, from, next) => {
            if (from.path == '/trade') {
                next()
            } else {
                next(false)
            }
        }
    },
    {
        path: '/paySuccess',
        component: PaySuccess,
        name: 'paysuccess',
        meta: {
            isShow: true
        },
        beforeEnter: (to, from, next) => {
            if (from.path == '/pay') {
                next()
            } else {
                next(false)
            }
        }
    },
    {
        path: '/center',
        component: Center,
        name: 'center',
        meta: {
            isShow: true
        },
        children: [{
                path: 'myorder',
                component: MyOrder,
                name: 'myorder',
            },
            {
                path: 'grouporder',
                component: GroupOrder,
                name: 'grouporder'
            },
            {
                path: '/center',
                redirect: '/center/myorder'
            }

        ]
    },
]